package com.koicarp.agent.example.chatmemery;

import com.koicarp.agent.example.ChatModelInit;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;


/**
 * @Author: liuxia
 * @CreateTime: 2025/9/13 下午11:11
 * @Description: 持久化聊天缓存test
 */
public class PersistentChatMemoryStoreTest {

    @Resource
    PersistentChatMemoryStore store;

    @Test
    public  void test() {
        ChatModel model = ChatModelInit.initOllama();

        MessageWindowChatMemory memory = MessageWindowChatMemory.builder()
                .id("1234")                 // id，通常用于区分多个用户或多个对话
                .maxMessages(4)             // 最大存储消息数量
                .chatMemoryStore(store)     // 持久化缓存存储类
                .build();

        UserMessage oneMsg = UserMessage.from("你好，你现在是我的朋友koi");
        memory.add(oneMsg);
        AiMessage oneAiMsg = model.chat(memory.messages()).aiMessage();
        memory.add(oneAiMsg);
        System.out.println("ai回答1： " + oneAiMsg.text());

        UserMessage twoMsg = UserMessage.from("你是我的朋友koi吗");
        memory.add(twoMsg);
        AiMessage twoAiMsg = model.chat(memory.messages()).aiMessage();
        memory.add(twoAiMsg);
        System.out.println("ai回答2： " + twoAiMsg.text());
        System.out.println("数据库已有对话数：" + memory.messages().size());
    }

}
